package com.rfsp.demo.controller;

import com.github.pagehelper.PageInfo;
import com.rfsp.common.base.ResultVO;
import com.rfsp.demo.pojo.vo.DemoResultVO;
import com.rfsp.mcp.controller.base.BaseController;
import com.rfsp.mcp.pojo.dto.FinanceCancelDTO;
import com.rfsp.mcp.pojo.dto.FinanceDTO;
import com.rfsp.mcp.pojo.sdto.FinanceSDTO;
import com.rfsp.common.pojo.sdto.PageSDTO;
import com.rfsp.mcp.pojo.sdto.ProductSDTO;
import com.rfsp.mcp.pojo.vo.BargainVO;
import com.rfsp.mcp.pojo.vo.FinanceVO;
import com.rfsp.mcp.pojo.vo.OrderVo;
import com.rfsp.mcp.pojo.vo.ProductVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller("demo2FinanceController")
@RequestMapping("/demo/finance")
class Demo2FinanceController extends BaseController {

    //索引页
    @RequestMapping("/")
    String index(ModelMap model, PageSDTO pageSdto, FinanceSDTO sdto) {
        model.put("sdto",sdto);
        PageInfo<FinanceVO> page=new PageInfo<FinanceVO>();

        FinanceVO vo=new FinanceVO();
        vo.setId(333333L);
        vo.setBankOrgId(444444L);
        vo.setFinanceNo("abc123456789");
        vo.setAccountBank("银行名称名称");
        vo.setFinanceStatus("1");
        List<FinanceVO> voList=new ArrayList<>();
        voList.add(vo);
        page.setList(voList);

        model.put("page",page);

        return "demo/finance/index";
    }


    /**
     * 详情
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/detail")
    String detail(ModelMap model,Long id) {

        FinanceVO vo=new FinanceVO();
        vo.setId(333333L);
        vo.setBankOrgId(444444L);
        vo.setProductName("详情aaa");
        vo.setBankOrgName("详情资金方aaa");
        vo.setAreaName("抚顺");
        vo.setOptType("0");
        //vo.setPublishTimeStr("05-30");
        //vo.setAmount("5w");
        //vo.setTimeLimit("09-30");
        //vo.setRate("0.4%");
        vo.setFinanceNo("1234567890");
        vo.setApplyTime(new Date());

        model.put("vo",vo);

        return "demo/finance/detail";
    }


    /**
     * 创建
     * @param model
     * @param dto
     * @param result
     * @return
     */
    @PostMapping("/create")
    String create(ModelMap model, @ModelAttribute("dto")  @Valid FinanceDTO dto, BindingResult result) {

        //失败流程
        return createForm(model,dto,result);

        //成功流程
        //return this.alertLocation(model,"操作成功","./");
    }

    /**
     * 创建表单
     * @param model
     * @param dto
     * @param bindingResult
     * @return
     */
    @GetMapping("/create")
    String createForm(ModelMap model, @ModelAttribute("dto") FinanceDTO dto, BindingResult bindingResult) {
        model.put("dto",dto);

        ResultVO<BargainVO> result = ResultVO.success(null);
        model.put("result", result);
        if(bindingResult.hasErrors()) {
            result.setMsg("操作失败");
        }

        return "demo/finance/form";
    }

    /**
     * 页面验证示例大全
     * @param model
     * @param dto
     * @param bindingResult
     * @return
     */
    @GetMapping("/create1")
    String createForm1(ModelMap model, @ModelAttribute("dto") FinanceDTO dto, BindingResult bindingResult) {
        model.put("dto",dto);

        ResultVO<BargainVO> result = ResultVO.success(null);
        model.put("result", result);
        if(bindingResult.hasErrors()) {
            result.setMsg("操作失败");
        }
        return "demo/finance/form1";
    }

    /**
     * 撤销
     * @param model
     * @param dto
     * @param result
     * @return
     */
    @PostMapping("/cancel")
    String cancel(ModelMap model, @ModelAttribute("dto")  @Valid FinanceCancelDTO dto, BindingResult result) {
        return this.alertParentReload(model,"操作成功");
        //return cancelForm(model,dto,result);
    }


    /**
     * 撤销
     * @param model
     * @param dto
     * @param bindingResult
     * @return
     */
    @GetMapping("/cancel")
    String cancelForm(ModelMap model, @ModelAttribute("dto") FinanceCancelDTO dto, BindingResult bindingResult) {
        model.put("dto",dto);

        ResultVO<BargainVO> result = ResultVO.success(null);
        model.put("result", result);
        if(bindingResult.hasErrors()) {
            result.setMsg("操作失败");
        }

        //手工加入错误提示
        result.setMsg("操作失败");
        bindingResult.addError(new FieldError("dto", "global", "添加失败，请联系管理员"));

        return "demo/finance/cancelForm";
    }

    /**
     * 产品选择器
     * @param model
     * @param pageSdto
     * @param sdto
     * @return
     */
    @RequestMapping("/productSelector")
    String productSelector(ModelMap model, PageSDTO pageSdto, ProductSDTO sdto) {

        //排序示例
        OrderVo orderVo=new OrderVo("desc",sdto.getPublishTimeOrder(),sdto.getPublishTimeOrderSubmit(),2);
        sdto.setPublishTimeOrder(orderVo.getValue());
        sdto.setPublishTimeOrderSubmit(orderVo.getSubmitValue());
        model.put("sdto",sdto);

        return "demo/finance/productSelector";
    }

    /**
     * 产品详情数据
     * @param model
     * @return
     */
    @RequestMapping("/productDetailData")
    @ResponseBody
    DemoResultVO<ProductVO> productDetailData(ModelMap model) {
        ProductVO vo=new ProductVO();
        vo.setId(11111L);
        vo.setBankOrgId(11112L);
        vo.setProductName("产品详情名称aaa");
        vo.setBankOrgName("产品详情资金方aaa");
        vo.setAreaName("抚顺");
        vo.setPublishTimeStr("05-30");
        vo.setProductName("aaa");
        vo.setAmount("5w");
        vo.setTimeLimit("09-30");
        vo.setRate("0.4%");
        return new DemoResultVO(vo);
    }

    /**
     * 产品详情页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/productDetail")
    String productDetail(ModelMap model,Long id) {
        model.put("vo",null);
        if(id!=null){
            ProductVO vo=new ProductVO();
            vo.setId(11111L);
            vo.setBankOrgId(11112L);
            vo.setProductName("产品详情名称aaa");
            vo.setBankOrgName("产品详情资金方aaa");
            vo.setAreaName("抚顺");
            vo.setPublishTimeStr("05-30");
            vo.setProductName("aaa");
            vo.setAmount("5w");
            vo.setTimeLimit("09-30");
            vo.setRate("0.4%");
            model.put("vo",vo);
        }
        return "demo/finance/productDetail";
    }


    /**
     * 操作方法提示示例
     * @param model
     * @param type
     * @return
     */
    @RequestMapping("/test")
    String test(ModelMap model, String type) {
        if("1".equals(type)){
            return this.alertParentReload(model,"操作成功");
        }
        if("2".equals(type)){
            return this.alertLocation(model,"操作成功","/mcp/demo/finance/create");
        }
        if("3".equals(type)){
            return this.alertBack(model,"操作失败");
        }
        return this.alertParentReload(model,"操作成功");
    }

    @RequestMapping("/dialog")
    String dialog(ModelMap model) {
        return "demo/finance/dialog";
    }

}
